/*
 * Text Message (A602 Open Server to Client) FMI PACKET ID: 0x0021
 * Mensaje de texto simple, sin estatus.
 */
package com.pressa.client.service.FMIPackets;

import java.math.BigInteger;

/**
 *
 * @author Lucas Borri (lborri@pressa.com.ar)
 */
public class FMI2100 {
    
    private static final String PacketID = "a1";    //0xa1.
    private static final String FMIPacketID = "2100";   //0x0021 Text Message Open From Server.
    private String messageText;     //Text Message to Send.
    private Long origTime;        //Original Time.
    private String packetToSend = null;    //Final packet to Send.
    private String origTimeString;
    private int status_packet;
    
    public FMI2100 (String message){    //Constructor para cuando entramos con el mensaje para armar el packet
    
        this.messageText = message;  
        this.origTime = (System.currentTimeMillis() / 1000) - 631065600L;        //   630720000
        this.makePacket();         //Make the packet to Send                
    }
    
    public FMI2100 (String pckt, int status){      //Constructor para cuando entramos con el packete ya armado
        packetToSend = pckt; 
        status_packet = status;
        origTimeString = packetToSend.substring(packetToSend.indexOf(",")+ 9, packetToSend.indexOf(",") + 17);
        
    }
    
    public void makePacket (){
        
        //Message to HEX + End (00)
        packetToSend = toHex(getMessageText()) + "00";
        
        //origTime to HEX and Inverse
        String timeString = Long.toHexString(getOrigTime());
        char[] temp3 = {timeString.charAt(6), timeString.charAt(7), timeString.charAt(4), timeString.charAt(5), timeString.charAt(2), timeString.charAt(3), timeString.charAt(0), timeString.charAt(1)};
        packetToSend = new String(temp3) + packetToSend;
        
        //Adding FMIPacketID     
        packetToSend = FMIPacketID + packetToSend;
        
        
        //Adding Payload
        String tempPayload = Integer.toHexString(packetToSend.length()/2);
        if ((packetToSend.length()/2) < 16){        //Correcion si el payload es de una solo caracter
            tempPayload = "0" + tempPayload;
        }
        packetToSend = tempPayload + packetToSend;
        
        //Adding PacketID
        packetToSend = PacketID + packetToSend;
        
        //Adding GTE Trax Line command
        packetToSend = ">SFM.," + packetToSend + "<";                           //Packet RDY to send
        
    }
    public String getPacket (){
        return this.packetToSend;
    }
    
    public void setPacket(String pckt){
        this.packetToSend = pckt;
    }
    
    String toHex(String arg) {  //metodo para convertir string a hexa
        return String.format("%x", new BigInteger(arg.getBytes(/*YOUR_CHARSET?*/)));
    }
    
    public String convertHexToString(String hex) {

        StringBuilder sb = new StringBuilder();
        StringBuilder temp = new StringBuilder();
        for (int i = 0; i < hex.length() - 1; i += 2) {
            String output = hex.substring(i, (i + 2)); //grab the hex in pairs
            int decimal = Integer.parseInt(output, 16); //convert hex to decimal
            sb.append((char) decimal); //convert the decimal to character
            temp.append(decimal);
        }
        return sb.toString();
    }

    /**
     * @return the messageText
     */
    public String getMessageText() {
        return messageText;
    }

    /**
     * @param messageText the messageText to set
     */
    public void setMessageText(String messageText) {
        this.messageText = messageText;
    }

    /**
     * @return the origTime
     */
    public Long getOrigTime() {
        return origTime;
    }

    /**
     * @param origTime the origTime to set
     */
    public void setOrigTime(Long origTime) {
        this.origTime = origTime;
    }

    /**
     * @return the origTimeString
     */
    public String getOrigTimeString() {
        return origTimeString;
    }
}
